package com.mice.microserviceconsumer.controller;

import com.mice.microserviceconsumer.bean.User;
import com.mice.microserviceconsumer.restApi.UserFeignClient;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.cloud.openfeign.encoding.BaseRequestInterceptor;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by AmbitiousMice on 2018/9/12.
 */
@Import(FeignClientsConfiguration.class)
@RestController
public class UserController {
    private UserFeignClient userUserFeignClient;

    private UserFeignClient adminUserFeignClient;

    @Autowired
    public UserController(Decoder decoder, Encoder encoder, Client client, Contract contract){
        this.userUserFeignClient = Feign.builder().client(client).encoder(encoder).decoder(decoder).client(client)
                .requestInterceptor(new BaseRequestInterceptor())
                .target(UserFeignClient.class,"http://microservice-provider-user/");
    }

    @RequestMapping("/user/{id}")
    public User findById(@PathVariable Integer id){

        return restTemplate.getForObject("http://127.0.0.1:8080/user/"+id,User.class);
    }
}
